package jdmx.framework.service.patterns.rough;

import java.sql.SQLException;

import jdmx.framework.common.Envelope;
import jdmx.framework.common.envelopes.EnvelopeRough;
import jdmx.framework.service.Dao;
import jdmx.library.da.DataTable;
import jdmx.library.da.IdList;
import jdmx.library.da.SqlStatement;

public class DaoRough extends Dao {
	@Override
	public void init(String handlingCode)
	{
		super.init(handlingCode);
		//
	}
	
	@Override
	public EnvelopeRough create(Envelope envelope) 
	{
		/*
		 * conversia la EnvelopeRough
		 */		
		if (!(envelope instanceof EnvelopeRough))
			return null;		
		EnvelopeRough requestEnvelope = (EnvelopeRough) envelope;
		EnvelopeRough resultEnvelope = new EnvelopeRough();
		
		/*
		 * extragere date
		 */
		SqlStatement sqlStatement = requestEnvelope.getSqlStatement();
		Integer id = null;
		try {
			id = db.execute(sqlStatement);
		} catch (SQLException e) {
		}
		
		/*
		 * impachetare si trimitere date
		 */
		IdList idList = new IdList();
		idList.add(id);
		resultEnvelope.setIdList(idList);
		return resultEnvelope;
	}

	@Override
	public EnvelopeRough read(Envelope envelope) 
	{
		/*
		 * conversia la EnvelopeRough
		 */		
		if (!(envelope instanceof EnvelopeRough))
			return null;		
		EnvelopeRough requestEnvelope = (EnvelopeRough) envelope;
		EnvelopeRough resultEnvelope = new EnvelopeRough();
		
		
		/*
		 * extragere date
		 */
		SqlStatement sqlStatement = requestEnvelope.getSqlStatement();
		DataTable dataTable = null;
		try {
			dataTable = db.fetch(sqlStatement);
		} catch (SQLException e) {
		}
		
		/*
		 * impachetare si trimitere date
		 */		
		if (dataTable != null) {
			resultEnvelope.setDatatable(dataTable);	
		}
		return resultEnvelope;
	}

	@Override
	public EnvelopeRough update(Envelope envelope) 
	{
		/*
		 * conversia la EnvelopeRough
		 */		
		if (!(envelope instanceof EnvelopeRough))
			return null;		
		EnvelopeRough requestEnvelope = (EnvelopeRough) envelope;
		
		/*
		 * extragere date
		 */
		SqlStatement sqlStatement = requestEnvelope.getSqlStatement();
		try {
			db.execute(sqlStatement);
		} catch (SQLException e) {
		}		
		
		return null;
	}
	
	@Override
	public EnvelopeRough delete(Envelope envelope)
	{
		/*
		 * conversia la EnvelopeRough
		 */		
		if (!(envelope instanceof EnvelopeRough))
			return null;		
		EnvelopeRough requestEnvelope = (EnvelopeRough) envelope;
		
		/*
		 * extragere date
		 */
		SqlStatement sqlStatement = requestEnvelope.getSqlStatement();
		try {
			db.execute(sqlStatement);
		} catch (SQLException e) {
		}		
		
		return null;
	}
}
